<?php

use yii\helpers\Url;
use yii\widgets\Pjax;

?>
    <div>
        <div>
            <button type="button" class="btn btn-danger z-delete">批量删除当前检索结果</button>
            <button type="button" class="btn btn-success send">批量分配当前检索结果</button>
        </div>
        <?php Pjax::begin(['id' => 'centerView']); ?>
        <?php if (isset($type)) {
            echo $this->render('_index_' . $type, ['dataProvider' => $dataProvider]);
        } ?>
        <?php Pjax::end(); ?>
    </div>
    <script>
        <?php $this->beginBlock('js_ok_remove') ?>
        let type_id = <?=$type?>;
        //移除
        $(document).on('click', '.z-delete', function () {
            let id = allChecked();
            let confirm_str = '';
            if (id) {
                id = JSON.stringify(id);
                confirm_str = '确认删除选中资源？';
            } else {
                confirm_str = '确认删除全部检索结果？';
            }
            parent.layer.confirm(confirm_str, {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-delete')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: id,
                        type_id, type_id
                    },
                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            parent.layer.msg(msg['msg'], {icon: 1, time: 1000});
                        } else {
                            parent.layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                        $.pjax.reload({container: "#centerView"});
                    },
                    error: function () {
                    }
                });
                parent.layer.close(index);
            });
        });
        //批量分配
        $(document).on('click', '.send', function () {
            let id = allChecked();

            parent.layer.open({
                type: 1,
                title: "分馆列表",
                area: ["350px", "160px"],
                content: parent.$('#open'),
            });
            //获取选项
            $.ajax({
                url: '<?=Url::toRoute('option')?>',//'/resource/resource/upload-file'
                type: 'GET',
                dataType: "json",
                success: function (msg) {

                    if (msg.status == 200) {
                        parent.$('#branch').html(msg.data);
                        parent.$('#rid').val(JSON.stringify(id));
                        parent.$('#type_id').val(type_id);
                    }
                },
                error: function () {
                }
            });
        });


        //获取复选框id
        function allChecked() {

            var id = [];
            var dom = $('.grid-view tbody').find('input[type=checkbox]:checked');
            if (dom.length) {
                $.each(dom, function () {
                    id.push($(this).val())
                });
            } else {
                return 0;
            }
            return id;
        }
        <?php $this->endBlock() ?>
    </script>
<?php $this->registerJs($this->blocks['js_ok_remove'], \yii\web\View::POS_END); ?>